package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.biz.service.core.banner.BannerService;
import com.treasure.mall.biz.service.core.module.ModuleService;
import com.treasure.mall.biz.vo.module.ModuleVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.ienum.banner.ModuleType;
import com.treasure.mall.zbq.common.ienum.base.CodeDescEnumHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallapi.controller
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/6/16 09:09
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * Copyright 2019-2022 com.treasure.mall.zbqmallapi.controller Inc. All rights reserved.
 */
@Api(tags = "banner模块")
@RestController
public class BannerController {

    @Resource
    private BannerService bannerService;
    @Autowired
    private ModuleService moduleService;
    @Autowired
    private BaseConfigService baseConfigService;


    @ApiOperation("获取banner列表")
    @GetMapping("/banner/list")
    public ApiResult list() {
        return ApiResult.ok(bannerService.getList());
    }


    /**
     * 广告位
     *
     * @param moduleType 0 首页广告位  1首页专区广告位
     * @return
     */
    @GetMapping("/banner/module/{moduleType}")
    public ApiResult homeModule(@PathVariable("moduleType") int moduleType) {

        ModuleType type = CodeDescEnumHelper.getEnumTypeByCode(ModuleType.class, moduleType);
        if (type == null) {
            return ApiResult.error("无效地址");
        }
        ModuleVO vo = moduleService.getConfig(type);
        if (vo != null) {
            return ApiResult.ok(vo.getAdConfig());
        }
        return ApiResult.error("无数据");
    }

    /**
     * 首页广告位
     *
     * @param
     * @return
     */
    @GetMapping("/banner/module/home")
    public ApiResult modules(@CurrentUser UserPO userPO) {
        ModuleVO home = moduleService.getConfig(ModuleType.MODULE_HOME);
        ModuleVO icons = moduleService.getConfig(ModuleType.MODULE_ICON);
        ModuleVO toDayNew = moduleService.getConfig(ModuleType.MODULE_TODAY_NEW);
        Map<String, Object> map = new HashMap<>(4);
        map.put("home", home != null ? home.getAdConfig() : null);
        map.put("icons", icons != null ? icons.getAdConfig() : null);
        map.put("todayNew", toDayNew != null ? toDayNew.getAdConfig() : null);
        map.put("djCount", baseConfigService.getZbConfig().getCurrentDjCount());
        return ApiResult.ok(map);
    }
}
